package org.ruizhijie.config;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.ruizhijie.i.ISubcriber;

public class TopicQueue {
	public static final EventLoopGroup group = new NioEventLoopGroup(5, new DefaultThreadFactory("udp_client", true));
	private Map<String,List<ISubcriber>> subs = new LinkedHashMap<String,List<ISubcriber>>();
	
	public void subcribe(ISubcriber is,String topic){
		System.err.println("subcribe"+topic);
		List<ISubcriber> iss = subs.get(topic);
		if (iss==null) iss = new ArrayList<ISubcriber>();
		iss.add(is);
		
		subs.put(topic, iss);
		
		System.out.println(subs);
	}
	
	public void unSubcribe(ISubcriber is,String topic){
		List<ISubcriber> iss = subs.get(topic);
		if (iss!=null)iss.remove(is);
	}
	
	/**
	 * 数据更新
	 */
	public void update(String topic,byte[] msg){
		System.err.println("update"+topic+"--"+msg);
		
		for (String key : subs.keySet()) {
			if (!topic.equals(key))continue;
			
			for (ISubcriber is : subs.get(key)) {
				is.onMessage(msg);
			}
		}
	}
}